package org.edu.modules.oa.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSort;
import groovy.util.logging.Slf4j;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.oa.entity.SysDictItemEntity;
import org.edu.modules.oa.entity.VeOaMeetAttenEntity;
import org.edu.modules.oa.mapper.IVeOaMeetAttenMapper;
import org.edu.modules.oa.service.IVeOaMeetAttenService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Api(tags = "会议管理-----考勤")
@RestController
@RequestMapping("oa/veOaMeetAtten")
@Slf4j
@ApiSort(value = 100)
public class VeOaMeetAttenController {

    @Resource
    private IVeOaMeetAttenService veOaMeetAttenService;


    @PostMapping("/loadMeetAtten")
    @ApiOperation(value="显示表格", notes="考勤---显示表格")
    public Result<?> loadMeetAtten(Integer start, Integer pageSize,String attenType,Integer meetId){
        System.out.println("start:"+start + "===attenType:"+attenType);
        HashMap<String,Object> map = veOaMeetAttenService.selectMeetAtten(start,pageSize,attenType,meetId);
        return Result.OK(map);
    }

    @PostMapping("/loadAttenType")
    @ApiOperation(value="显示表格", notes="考勤---下拉框显示")
    public Result<?> loadAttenType(){
        List<SysDictItemEntity> list = veOaMeetAttenService.selectAttenType();
        return Result.OK(list);
    }

    @PostMapping("/updateAttenType")
    @ApiOperation(value="显示表格", notes="考勤---修改考勤状态多条")
    public Result<?> updateAttenType(String ids,String attenType){
        VeOaMeetAttenEntity veOaMeetAttenEntity = new VeOaMeetAttenEntity();
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String typeId = null;
        if(attenType.equals("正常")){
            typeId = "1384319902448906242";
        }
        if(attenType.equals("迟到")){
            typeId = "1384319949848735745";
        }
        if(attenType.equals("早退")){
            typeId = "1384320061211701250";
        }
        if(attenType.equals("未到")){
            typeId = "1384320144628019202";
        }
        veOaMeetAttenEntity.setAttenType(typeId);
        veOaMeetAttenEntity.setAttenName(attenType);
        veOaMeetAttenEntity.setCreateBy(user.getId());
        veOaMeetAttenEntity.setCreateName(user.getRealname());
        String [] arr = ids.split(",");
        for (String id : arr) {
            if(!id.equals("") && id != null){
                veOaMeetAttenEntity.setId(Integer.parseInt(id));
                veOaMeetAttenService.updateAttenType(veOaMeetAttenEntity);
            }
        }
        return Result.OK();
    }

    @PostMapping("/updateAttenOne")
    @ApiOperation(value="显示表格", notes="考勤---修改考勤状态单条")
    public Result<?> updateAttenOne(Integer id,String attenType,String typeId,String remarks){
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        VeOaMeetAttenEntity veOaMeetAttenEntity = new VeOaMeetAttenEntity();
        veOaMeetAttenEntity.setId(id);
        veOaMeetAttenEntity.setAttenType(typeId);
        veOaMeetAttenEntity.setAttenName(attenType);
        veOaMeetAttenEntity.setCreateBy(user.getId());
        veOaMeetAttenEntity.setCreateName(user.getRealname());
        veOaMeetAttenEntity.setRemarks(remarks);
        System.out.println(veOaMeetAttenEntity);
        int res = veOaMeetAttenService.updateAttenType(veOaMeetAttenEntity);
        if(res == 0 ){
            return Result.error(500,"修改考勤状态失败了");
        }
        return Result.OK();
    }

}
